package com.gxgeek.javabasic.jdbc;

import java.sql.*;

/**
 * Created by gx on 2017/7/10.
 */
public class JDBCbase {
    static String driver = "com.mysql.jdbc.Driver";
    static String url = "jdbc:mysql://localhost:3306/test";
    static String username = "root";
    static String password = "123";


    public static void main(String[] args) throws SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
            String sql = "SELECT  * from person limit 1";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            ResultSetMetaData rsmd = resultSet.getMetaData();
            for (int i = 0; i < rsmd.getColumnCount(); i++) {
                System.out.println(rsmd.getColumnLabel(i+1));
            }

            while (resultSet.next()) {
//                System.out.println();

                System.out.println(resultSet.getInt("id"));
            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            try {
                if (resultSet != null) {

                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (preparedStatement != null) {

                    preparedStatement.close();
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (connection != null) {

                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


//        preparedStatement.
    }
}
